﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.DynamicData;

namespace FlexiCommerce.Web.Admin.FlexiAdmin.DynamicData.PageTemplates
{
    public partial class List : System.Web.UI.MasterPage
    {
        protected MetaTable table;

        protected void Page_Init(object sender, EventArgs e)
        {
            table = GridQueryExtender.SetTableFromRoute();
            bool value = table.CanInsert(HttpContext.Current.User);
            //// GridDataSource.EntityTypeName = table.EntityType.AssemblyQualifiedName;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Title = table.DisplayName;


            // Disable various options if the table is readonly
            if (table.IsReadOnly)
            {
                GridView1.Columns[0].Visible = false;
                InsertHyperLink.Visible = false;
                // GridView1.EnablePersistedSelection = false;
            }

            if (!CanInsert())
                InsertHyperLink.Visible = false;
        }
        public bool CanInsert()
        {
            bool value = table.CanInsert(HttpContext.Current.User);
            return value;
        }
        protected bool CanUpdate()
        {
            return table.CanUpdate(System.Web.HttpContext.Current.User);
        }
        protected bool CanDelete()
        {
            return table.CanDelete(System.Web.HttpContext.Current.User);
        }
        protected void Label_PreRender(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            DynamicFilter dynamicFilter = (DynamicFilter)label.FindControl("DynamicFilter");
            QueryableFilterUserControl fuc = dynamicFilter.FilterTemplate as QueryableFilterUserControl;
            if (fuc != null && fuc.FilterControl != null)
            {
                label.AssociatedControlID = fuc.FilterControl.GetUniqueIDRelativeTo(label);
            }
        }

        protected void DynamicFilter_FilterChanged(object sender, EventArgs e)
        {
            GridView1.PageIndex = 0;
        }
    }
}
